﻿namespace RuoVea.Swms.Web.Controllers;

    /// <summary>
    /// 出库管理
    /// </summary>
    [Authorize(Roles = "None")]
public class StockOutController : Controller
{
    private readonly InventoryService _thisService;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="inventoryService"></param>
    public StockOutController(InventoryService inventoryService)
    {
        _thisService = inventoryService;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public IActionResult index()
    {
        return View();
    }

    /// <summary>
    /// 出库记录分页查询
    /// </summary>
    [HttpPost("inventory/stockOut/pages")]
    public async Task<RestfulResult<PageResult<WmsStockOut>>> GetStockOutPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception("未提供上传数据。请在Request.Body中携带上传数据。");
        StockOutPageParam? data = JsonConvert.DeserializeObject<StockOutPageParam>(dataStr);
        data ??= new StockOutPageParam { PageNo = 1, PageSize = 10 };

        var query = await _thisService.GetStockOutPagesAsync(data);
        return query;
    }

    /// <summary>
    /// 创建出库单
    /// </summary>
    [UnitOfWork]
    [HttpPost("inventory/stockOut/add")]
    public async Task<RestfulResult> CreateStockOutAsync([FromBody] WmsStockOut data)
    {
        var restful = await _thisService.CreateStockOutAsync(data);
        return restful;
    }

    /// <summary>
    /// 获取当前库存
    /// </summary>
    [HttpPost("inventory/current")]
    public async Task<RestfulResult<List<WmsProduct>>> GetCurrentInventory()
    {
        var restful = await _thisService.GetCurrentInventory();
        return restful;
    }

    /// <summary>
    /// 获取库存变动历史
    /// </summary>
    [HttpPost("inventory/history")]
    public async Task<RestfulResult<List<WmsInventoryHistory>>> GetInventoryHistory([FromBody] InventoryHistoryParam param)
    {
        var restful = await _thisService.GetInventoryHistory(param.ProductId);
        return restful;
    }
}